﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using CalcSharp.Core;
using System.Runtime.Serialization.Formatters.Binary;
using CalcSharp.Core.Containers;
using CalcSharp.Core.Memory;

namespace CalcSharp.MDIUI
{
    public partial class LoadWindow : Form
    {
        private string fpath;
        private MemoryDumpReader mr;

        public LoadWindow(string filepath)
        {
            fpath = filepath;
            if (string.IsNullOrEmpty(filepath)) throw new ArgumentException("filepath can't be null or empty");
            if (!File.Exists(filepath)) throw new CSException("File doesn't exist: " + filepath);
            InitializeComponent();
            mr = new MemoryDumpReader(filepath);
        }

        private void DrawRegion(DumpDataType region)
        {
            this.ClbSectItems.Items.Clear();
            this.ClbSectItems.Items.AddRange(mr.GetVariablesOfType(region));
        }

        private void DrawRegions()
        {
            TreeNode rootnode = new TreeNode(Path.GetFileName(fpath));
            TreeNode work;
            string[] tmp;
            tmp = mr.GetVariablesOfType(DumpDataType.Float);
            if (tmp.Length > 0)
            {
                work = new TreeNode("Float (" + tmp.Length + ")");
                rootnode.Nodes.Add(work);
            }
            tmp = mr.GetVariablesOfType(DumpDataType.Complex);
            if (tmp.Length > 0)
            {
                work = new TreeNode("Complex (" + tmp.Length + ")");
                rootnode.Nodes.Add(work);
            }
            tmp = mr.GetVariablesOfType(DumpDataType.Matrix);
            if (tmp.Length > 0)
            {
                work = new TreeNode("Matrix(" + tmp.Length + ")");
                rootnode.Nodes.Add(work);
            }
            tmp = mr.GetVariablesOfType(DumpDataType.Set);
            if (tmp.Length > 0)
            {
                work = new TreeNode("Set (" + tmp.Length + ")");
                rootnode.Nodes.Add(work);
            }
            tmp = mr.GetVariablesOfType(DumpDataType.String);
            if (tmp.Length > 0)
            {
                work = new TreeNode("String (" + tmp.Length + ")");
                rootnode.Nodes.Add(work);
            }
            tmp = mr.GetVariablesOfType(DumpDataType.Function);
            if (tmp.Length > 0)
            {
                work = new TreeNode("Function (" + tmp.Length + ")");
                rootnode.Nodes.Add(work);
            }
            tmp = null;
            this.TvDataSect.Nodes.Clear();
            this.TvDataSect.Nodes.Add(rootnode);
            rootnode.Expand();
        }

        private void TvDataSect_DoubleClick(object sender, EventArgs e)
        {
            if (TvDataSect.SelectedNode != null)
            {
                if (TvDataSect.SelectedNode.Text.Contains("Float")) this.DrawRegion(DumpDataType.Float);
                else if (TvDataSect.SelectedNode.Text.Contains("Complex")) this.DrawRegion(DumpDataType.Complex);
                else if (TvDataSect.SelectedNode.Text.Contains("Matrix")) this.DrawRegion(DumpDataType.Matrix);
                else if (TvDataSect.SelectedNode.Text.Contains("Set")) this.DrawRegion(DumpDataType.Set);
                else if (TvDataSect.SelectedNode.Text.Contains("String")) this.DrawRegion(DumpDataType.String);
                else if (TvDataSect.SelectedNode.Text.Contains("Function")) this.DrawRegion(DumpDataType.Function);
            }
        }
    }
}
